<template>
  <main
    style="
      width: 100%;
      height: 280px;
      background-color: rgba(255, 255, 203, 0.5);
    "
  >
    <div style="width: 100%; height: 32px">
      <div>
        <el-input
          v-model="opruleForm.name"
          style="width: 100%"
          placeholder="请输入曲名"
          class="input-with-select"
        >
          <template #prepend>
            <el-select
              v-model="opruleForm.musickeys"
              placeholder="类型"
              style="width: 75px"
            >
              <el-option label="15键" value="1" />
              <el-option label="21键" value="2" />
            </el-select>
          </template>
          <template #append>
            <el-button :icon="Search" @click="onSearch()" />
            <el-button
              :icon="RefreshRight"
              style="margin-left: 15px"
              @click="deli()"
            />
          </template>
        </el-input>
      </div>
    </div>
    <div style="width: 100%; height: 224px; background-color: antiquewhite">
      <el-table
        :data="tableData"
        style="width: 100%; height: 100%"
        size="small"
        empty-text="暂无文件"
      >
        <el-table-column
          label="歌曲名称"
          width="210"
          align="left"
          show-overflow-tooltip
        >
          <template #default="scope">
            <div style="display: flex; align-items: center">
              <span style="margin-left: 10px">{{ scope.row.name }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button size="small" @click="handleEdit(scope.row)">
              试听
            </el-button>
            <el-button
              :icon="CaretRight"
              type="success"
              size="small"
              circle
              plain
              @click="handleDelete(scope.row)"
            />
            <!-- <el-button size="small" type="success" plain @click="handleDelete(scope.$index, scope.row)">
                            弹奏
                        </el-button> -->
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div
      style="
        width: 100%;
        height: 24px;
        background-color: antiquewhite;
        display: flex;
        justify-content: center;
        align-items: center;
      "
    >
      <el-pagination
        background
        size="small"
        layout="prev, pager, next"
        :total="pagination.total"
        :pager-count="5"
        v-model:page-size="pagination.pageSize"
        v-model:current-page="pagination.currentPage"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </main>
</template>

<script lang="ts" setup>
import { Search, RefreshRight, CaretRight } from "@element-plus/icons-vue";
import { ref, reactive, onMounted, toRaw } from "vue";
import { getAllMusic, getMusicById } from "@/api/reqapi";
import { message } from "@/utils/messages";
import { useRouter } from "vue-router";

const router = useRouter();
const opruleForm = reactive({
  musickeys: "1",
  name: "",
});
const pagination = reactive({
  total: 0,
  pageSize: 10,
  currentPage: 1,
});
const deli = () => {
  opruleForm.name = "";
  onSearch();
};
const handleEdit = async (row: any) => {
  const backlogin = reactive({
    id: row.id,
  });
  const res_rep_data: any = await getMusicById(backlogin);
  // 返回当前选中的行
  if (res_rep_data != null && res_rep_data.code == "1000") {
    if (
      res_rep_data.content.spectrum &&
      typeof res_rep_data.content.spectrum === "string"
    ) {
      try {
        onautdio(res_rep_data.content.spectrum);
      } catch (e) {
        message("请求异常", "warning");
      }
    } else {
      message("非合法 TXT 谱，重新选择", "warning");
    }
  }
};
const handleDelete = async (row: any) => {
  const backlogin = reactive({
    id: row.id,
  });
  const res_rep_data: any = await getMusicById(backlogin);
  // 返回当前选中的行
  if (res_rep_data != null && res_rep_data.code == "1000") {
    console.log(res_rep_data.content.spectrum);
    if (
      res_rep_data.content.spectrum &&
      typeof res_rep_data.content.spectrum === "string"
    ) {
      try {
        onFileContent(res_rep_data.content.spectrum);
      } catch (e) {
        console.log("请求异常", "warning");
      }
    } else {
      message("非合法 TXT 谱，重新选择", "warning");
    }
  }
};
function onFileContent(content: string) {
  try {
    router.push({
      path: "/music",
      query: { content },
    });
  } catch (e) {
    message("非合法 TXT 谱，重新选择", "warning");
  }
}
function onautdio(content: string) {
  try {
    router.push({
      path: "/onautdio",
      query: { content },
    });
  } catch (e) {
    message("非合法 TXT 谱，重新选择", "warning");
  }
}
const onSearch = () => {
  try {
    const dataResult: DataResult = {
      code: "1000",
      pagesize: pagination.currentPage,
      content: toRaw(opruleForm),
      current: pagination.pageSize,
    };
    getAllMusic(dataResult).then((res_rep_data: any) => {
      if (res_rep_data != null && res_rep_data.code == "1000") {
        tableData.value = res_rep_data.content;
        pagination.total = res_rep_data.pages;
      }
    });
  } catch (error) {}
};
const handleSizeChange = (val: number) => {
  onSearch();
};
const handleCurrentChange = (val: number) => {
  onSearch();
};
onMounted(() => {
  onSearch();
});
const tableData = ref([]);
</script>
